<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	$auth = Zend_Auth::getInstance();
    	$infoStd = $auth->getIdentity();
    	if(isset($infoStd)){
    		$this->view->StdName = $infoStd->student_name;    		
    	}
    }

    public function indexAction()
    {
        // action body
    }

    public function loginAction()
    {
        // action body
        //$this->_helper->layout()->disableLayout();
        $auth = Zend_Auth::getInstance();
    if($this->getRequest()->isPost()){
			$db = Zend_Registry::get("connectDb");
			$authAdapter = new Zend_Auth_Adapter_DbTable($db);
			$authAdapter->setTableName("students")->setIdentityColumn("student_username")->setCredentialColumn("student_password");
			
			$uName = $this->_request->getParam("stdName");
			$Pass = $this->_request->getParam("password");
			
			$authAdapter->setIdentity($uName);
			$authAdapter->setCredential(md5("YSemina".md5($Pass)));
			
			$select = $authAdapter->getDbSelect();
			$select->where("status = 0");
			
			$result = $auth->authenticate($authAdapter);
			$flag = false;
			if($result->isValid()){
				$data = $authAdapter->getResultRowObject(null,array("student_password"));
				$auth->getStorage()->write($data);
				$flag = true;
			}else{
				$this->view->error = "Wrong User Name Or Password!";
			}
			if($flag == true){
				$this->_redirect("index");
			}
		}
		if($auth->hasIdentity()){
			$this->_redirect("index");
		}
    }

    public function logoutAction()
    {
        // action body
    	$auth = Zend_Auth::getInstance();
    	$auth->clearIdentity();
    	$this->_redirect("/admin/index/login");
    }


}





